package javanlp;

import java.io.*;
import opennlp.tools.chunker.*;
import opennlp.tools.cmdline.*;
import opennlp.tools.cmdline.postag.*;
import opennlp.tools.postag.*;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.*;

public class stChunk{
public static void main(String[] args) throws IOException {
    String fld = "C:/Users/lenovolap/Dropbox/java/NLP.models/";
 String trainedmodel =fld+"en-pos-maxent.zip";
	POSModel model = new POSModelLoader().load(new File(trainedmodel));
	PerformanceMonitor perfMon = 
                        new PerformanceMonitor(System.err, "sent");
	POSTaggerME tagger = new POSTaggerME(model);
 	String input = "Hi. How are you? This is Mike.";
	ObjectStream<String> lineStream = 
                new PlainTextByLineStream(new StringReader(input));
 	perfMon.start();
	String line;
	String[] wsTokenizerLine = null;
	String[] tags = null;
	while ((line = lineStream.read()) != null) {
		wsTokenizerLine = WhitespaceTokenizer.INSTANCE.tokenize(line);
		tags = tagger.tag(wsTokenizerLine);
 		POSSample sample = new POSSample(wsTokenizerLine, tags);
		System.out.println(sample.toString());
		perfMon.incrementCounter();
		}
		perfMon.stopAndPrintFinalResult();

		// chunker
                String trainedmodels =fld+"en-chunker.zip";
		InputStream is = new FileInputStream(trainedmodels);
		ChunkerModel cModel = new ChunkerModel(is);
 
		ChunkerME chunkerME = new ChunkerME(cModel);
		String result[] = chunkerME.chunk(wsTokenizerLine, tags);
 
		for (String s : result){System.out.println(s);}
 
		Span[] span = chunkerME.chunkAsSpans(wsTokenizerLine, tags);
		for (Span s : span){System.out.println(s.toString());}
 
	}
}